# 文本降重
import requests as rq
import json


def do_post(sentence):
    url = 'http://192.168.6.254:8109/reduce'  # 需要请求的URL地址
    data = {'text': sentence.strip()}  # POST请求需要提交的数据
    data = json.dumps(data)  # 有的时候data需要时json类型的
    headers = {'content-type': 'application/json'}  # 一种请求头，需要携带
    res = rq.post(url=url, data=data, headers=headers)  # 发起请求
    target = res.json()  # 将获取到的数据变成json类型
    return target


def handle(infile, outfile):
    fp = open(infile, 'r', encoding='utf-8')
    fout = open(outfile, 'w', encoding='utf-8')
    lines = fp.readlines()
    for line in lines:
        reduce_sentence = do_post(line)
        result_seq = reduce_sentence["text"]
        print(result_seq)
        fout.write(result_seq + "\n")


if __name__ == "__main__":
    infile = 'g:/0812test1.txt'
    outfile = 'g:/0812test1_u.txt'
    handle(infile, outfile)
